<?php

namespace App\Models;


use App\Services\Base\CacheService;
use Illuminate\Database\Eloquent\Model;

class ContentCategory extends Model
{
    protected $table = 'content_category';
    protected $dateFormat = 'U';

    //
    public function content()
    {
        return $this->hasOne(Content::class);
    }

    public static function getlist($id)
    {
        $data = self::whereNull('deleted_at')->whereNotIn('id', $id)->select('sn as ids', 'name as text')->get()->toArray();
        return $data;
    }

    public function getlistById($id)
    {
        $data = self::whereNull('deleted_at')->where('primary_id', $id)->select('sn as ids', 'name as text')->get()->toArray();
        return $data;
    }
//    public function false_del($id){
//        $result = self::where('id', $id)->update(['deleted_at' => time()]);
//        return $result;
//    }
    public static function getfield($where)
    {
        $data = self::where($where)->select('name')->first();
        return $data['name'] ?? "";
    }

    public static function listByCondition($where)
    {
        $data = self::whereNull('deleted_at')->where($where)->select('sn as ids', 'name as text')->get()->toArray();
        return $data;
    }

    public function loadForHelp()
    {
        $key = "helps";
        $val = CacheService::get($key);
        if (!$val) {
            $val = self::query()->orderBy('weight', 'desc')->get()->keyBy('id')->toArray();
            if ($val) {
                CacheService::set($key, $val, 1);
            }
        }
        return false;
    }

    public function getChildren($id)
    {
        $data = self::whereNull('deleted_at')->where('primary_id', $id)->select('sn as ids', 'name as text', 'primary_id')->get()->toArray();
        return $data;
    }

//    public function getChildren($id){
//        $data = self::whereNull('deleted_at')->whereRaw('primary_id in (SELECT sn from content_category where primary_id = ?)', [$id])->select('sn as ids', 'name as text','primary_id')->get()->toArray();
//        return $data;
//    }
    public static function getCatChildren($id)
    {
        $data = self::whereNull('deleted_at')->where('primary_id', $id)->pluck('sn')->toArray();
        return $data;
    }

    public static function getFirstCat($id)
    {
        $data = self::whereNull('deleted_at')->whereRaw('primary_id in (SELECT sn from content_category where primary_id = ?)', [$id])->pluck('sn')->toArray();
        return $data;
    }

    public function getListForShow($id)
    {
        $data = self::whereNull('deleted_at')->where('primary_id', $id)->where('show', 1)->select('sn as ids', 'name as text')->get()->toArray();
        return $data;
    }

    public function parent()
    {
        return $this->belongsTo(ContentCategory::class, 'primary_id', 'id');
    }

    public static function getSelectList()
    {
        $result = self::query()->where('show', STATUS_ENABLED)
            ->select('id', 'sn', 'name', 'lang')
            ->get()
            ->toArray();
        $new_result = [];
        foreach ($result as $k => $v) {
            $new_result[$v['sn']] = $v['name'] . "(" . Content::$lib_lang[$v['lang']] . ")";
        }

        return $new_result;
    }

    public static function oneAsNotification()
    {
        return self::query()->whereNull('deleted_at')
            ->where('sn', 8004)
            ->orderByDesc('id')
            ->first();
    }

    public static function oneAsTeam()
    {
        return self::query()->whereNull('deleted_at')
            ->where('sn', 8008)
            ->orderByDesc('id')
            ->first();
    }
}
